package com.GlobalPaint.app.ui.Mine;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Base.DialogWaiting;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.PostTalentBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.bean.TalentBean;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.GlobalPaint.app.R.id.ed_name;

public class ResumeActivity extends AppCompatActivity {

    @BindView(R.id.iv_goback)
    ImageView ivGoback;
    @BindView(R.id.tv_common_center)
    TextView tvCommonCenter;
    @BindView(R.id.tv_fresh)
    TextView tvFresh;
    @BindView(ed_name)
    EditText edName;
    @BindView(R.id.et_sex)
    EditText etSex;
    @BindView(R.id.et_education)
    EditText etEducation;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_birthdate)
    EditText etBirthdate;
    @BindView(R.id.et_post)
    EditText etPost;
    @BindView(R.id.et_salary)
    EditText etSalary;
    @BindView(R.id.tv_work)
    EditText tvWork;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.tv_common)
    TextView tvCommon;
    @BindView(R.id.education)
    TextView education;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.email)
    TextView email;
    @BindView(R.id.birthdate)
    TextView birthdate;
    @BindView(R.id.post)
    TextView post;
    @BindView(R.id.salary)
    TextView salary;
    @BindView(R.id.scroll_view)
    ScrollView scrollView;
    @BindView(R.id.activity_resume)
    LinearLayout activityResume;
    private String activity;//接受的数据 人才库  和 简历
    private Dialog dlgWaiting2;
    private TalentBean bean;
    private PostTalentBean postTalentBean;
    String posturl ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resume);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        if (getIntent().getStringExtra("activity").equals("talent")) {
            ArrayList<String> infoList = new ArrayList<String>();
            infoList = getIntent().getStringArrayListExtra("infoList");
            String name = infoList.get(0) == null ? "" : infoList.get(0);
            String Sex = infoList.get(1) == null ? "" : infoList.get(1);
            String Education = infoList.get(2) == null ? "" : infoList.get(2);
            String Phone = infoList.get(3) == null ? "" : infoList.get(3);
            String Email = infoList.get(4) == null ? "" : infoList.get(4);
            String Birthdate = infoList.get(5) == null ? "" : infoList.get(5);
            String Post = infoList.get(6) == null ? "" : infoList.get(6);
            String Salary = infoList.get(7) == null ? "" : infoList.get(7);
            String Work = infoList.get(8) == null ? "" : infoList.get(8);
            String Remark = infoList.get(9) == null ? "" : infoList.get(9);
            edName.setText(name);
            if(Sex.equals("M")){
                etSex.setText("男");
            }else {
                etSex.setText("女");
            }

            etEducation.setText(Education);
            if (Phone.length() > 4) {
                String phone1 = (Phone.substring(0, 3));
                String phone2 = Phone.substring(Phone.length() - 3, Phone.length());
                etPhone.setText(phone1 + "***" + phone2);
            }
            etEmail.setText(Email);
            if (Birthdate.length() > 10) {
                etBirthdate.setText(Birthdate.substring(0, 10));
            }

            etPost.setText(Post);
            etSalary.setText(Salary);
            tvWork.setText(Work);
            etRemark.setText(Remark);
            tvCommonCenter.setText("简历详情");
        } else if (getIntent().getStringExtra("activity").equals("MineFragment")) {
            tvCommonCenter.setText("个人简历");
            tvFresh.setVisibility(View.VISIBLE);

            getReume();
        }

    }

    private void getReume() {
        dlgWaiting2 = DialogWaiting.createDialogWaiting(ResumeActivity.this, "加载中......");
        dlgWaiting2.show();
        String url = Constants.JASON_SERVICE_URL + "/renren-security/appapi/resume/query?fUserId=7";
        DataManager.getInst().getHttpRequestJsonClass(url, "dd", TalentBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                dlgWaiting2.cancel();
                bean = (TalentBean) o;
                if (bean.getStatus() == 1) {
                    if (bean.getData() != null && bean.getData().size() > 0) {
                        tvFresh.setText("编辑");
                        String name = bean.getData().get(0).getFName() ==
                                null ? "" : bean.getData().get(0).getFName();
                        String Sex = bean.getData().get(0).getFGender() ==
                                null ? "" : bean.getData().get(0).getFGender();
                        String Education = bean.getData().get(0).getFEducationExperience() ==
                                null ? "" : bean.getData().get(0).getFEducationExperience();
                        String Phone = bean.getData().get(0).getFMobile() ==
                                null ? "" : bean.getData().get(0).getFMobile();
                        String Email = bean.getData().get(0).getFEmail() ==
                                null ? "" : bean.getData().get(0).getFEmail();
                        String Birthdate = bean.getData().get(0).getFBirthDate() ==
                                null ? "" : bean.getData().get(0).getFBirthDate();
                        String Post = bean.getData().get(0).getFPost() ==
                                null ? "" : bean.getData().get(0).getFPost();
                        String Salary = bean.getData().get(0).getFExpectedSalary() ==
                                null ? "" : bean.getData().get(0).getFExpectedSalary();
                        String Work = bean.getData().get(0).getFWorkExperience() ==
                                null ? "" : bean.getData().get(0).getFWorkExperience();
                        String Remark = bean.getData().get(0).getFRemark() ==
                                null ? "" : bean.getData().get(0).getFRemark();
                        edName.setText(name);
                        if(Sex.equals("M")){
                            etSex.setText("男");
                        }else {
                            etSex.setText("女");
                        }
                        etEducation.setText(Education);
                        if (Phone.length() > 4) {
                            String phone1 = (Phone.substring(0, 3));
                            String phone2 = Phone.substring(Phone.length() - 3, Phone.length());
                            etPhone.setText(phone1 + "***" + phone2);
                        }
                        etEmail.setText(Email);
                        if (Birthdate.length() > 10) {
                            etBirthdate.setText(Birthdate.substring(0, 10));
                        }

                        etPost.setText(Post);
                        etSalary.setText(Salary);
                        tvWork.setText(Work);
                        etRemark.setText(Remark);
                    }else {
                        tvFresh.setText("新增");
                    }
                } else {

                    Toast.makeText(ResumeActivity.this, bean.getMsg(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                dlgWaiting2.cancel();

                Toast.makeText(ResumeActivity.this, "获取失败", Toast.LENGTH_LONG).show();
            }
        });
    }

    @OnClick({R.id.iv_goback, R.id.tv_fresh, R.id.tv_common})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_goback:
                finish();
                break;
            case R.id.tv_fresh:
                Edit();
                break;
            case R.id.tv_common:
                postResume();
                break;
        }
    }
    private void Edit(){
        edName.setFocusable(true);
        edName.setFocusableInTouchMode(true);
        edName.requestFocus();
        etSex.setFocusable(true);
        etSex.setFocusableInTouchMode(true);
        etEducation.setFocusable(true);
        etEducation.setFocusableInTouchMode(true);
        etPhone.setFocusable(true);
        etPhone.setFocusableInTouchMode(true);
        etEmail.setFocusable(true);
        etEmail.setFocusableInTouchMode(true);
        etBirthdate.setFocusable(true);
        etBirthdate.setFocusableInTouchMode(true);
        etPost.setFocusable(true);
        etPost.setFocusableInTouchMode(true);
        etSalary.setFocusable(true);
        etSalary.setFocusableInTouchMode(true);
        tvWork.setFocusable(true);
        tvWork.setFocusableInTouchMode(true);
        etRemark.setFocusable(true);
        etRemark.setFocusableInTouchMode(true);
        tvCommon.setVisibility(View.VISIBLE);
    }
    private void postResume(){

        if(tvFresh.getText().toString().equals("新增")){
            posturl = Constants.JASON_SERVICE_URL + "/renren-security/appapi/resume/save";
        }else if(tvFresh.getText().toString().equals("编辑")){
            posturl = Constants.JASON_SERVICE_URL + "/renren-security/appapi/resume/update";
        }
        postTalentBean=new PostTalentBean();
        postTalentBean.setFName(edName.getText().toString());
        postTalentBean.setFGender(etSex.getText().toString());
        postTalentBean.setFEducationExperience(etEducation.getText().toString());
        postTalentBean.setFMobile(etPhone.getText().toString());
        postTalentBean.setFEmail(etEmail.getText().toString());
        postTalentBean.setFBirthDate(etBirthdate.getText().toString());
        postTalentBean.setFPost(etPost.getText().toString());
        postTalentBean.setFExpectedSalary(etSalary.getText().toString());
        postTalentBean.setFWorkExperience(tvWork.getText().toString());
        postTalentBean.setFRemark(etRemark.getText().toString());
        postTalentBean.setFUserId("7");
        DataManager.getInst().postHttpRequestJsonClass(posturl, "dd", postTalentBean, TalentBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean = (TalentBean) o;
                if(bean.getStatus()==1){
                    Toast.makeText(ResumeActivity.this, "提交成功", Toast.LENGTH_LONG).show();
                    tvCommon.setVisibility(View.GONE);
                }else {
                    Toast.makeText(ResumeActivity.this, bean.getMsg(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                Toast.makeText(ResumeActivity.this, "提交失败", Toast.LENGTH_LONG).show();
            }
        });
    }
    //隐藏软键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, ev)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    // 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            return !(event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom);
        }
        return false;
    }

}
